<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column3';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    //public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();

    /**
     * Use in tab menu and side bar. 
     * Array of categories. Each $category is an aray which is in form that:
     *     array('id'       => %TYPE_ID%,
     *           'name'     => %TYPE_NAME%,
     *           'childCategories' => array(
     *                   array('id'   => %CATEGORY_ID1%, 
     *                           'name' => %CATEGORY_NAME1%),
     *                   array('id'   => %CATEGORY_ID2%, 
     *                           'name' => %CATEGORY_NAME2%)));
     * @var array
     */
    public $metaDescription;
    public $categories = array();
    public $subCategories = array();

    /**
     * 
     * @var array
     */
    public $languages = array('default' => 'en', 'supported' => array('vi', 'en'));

    /**
     * 
     */
    public function __construct($id, $module = NULL) {
        parent::__construct($id, $module);

        $this->initLanguage();
        $this->initCategories(Yii::app()->language);
    }

    /**
     * 
     */
    public function initLanguage() {
        $language = Yii::app()->session['language'];
        if (isset($language) && in_array($language, $this->languages['supported'])) {
            Yii::app()->language = $language;
        } else {
            Yii::app()->language = $this->languages['default'];
        }
    }

    /**
     * 
     * @param unknown_type $language
     */
    public function initCategories($language) {
        $results = Yii::app()->db->createCommand()
                ->selectDistinct('c.id id , cd.name name, cd.seokey seokey')
                ->from('category c')
                ->leftJoin('category_detail cd', 'c.id = cd.category_id')
                ->where('cd.language = :language AND c.parent_id is null AND c.is_displayed = 1', array(':language' => $language))
                ->queryAll();
        if (count($results) == 0) {
            return;
        }

        $categories = array();
        foreach ($results as $category) {
            $cats = Yii::app()->db->createCommand()
                    ->selectDistinct('c.id id , cd.name name, cd.seokey seokey')
                    ->from('category c')
                    ->leftJoin('category_detail cd', 'c.id = cd.category_id')
                    ->where('cd.language = :language AND c.parent_id = :parent_id AND c.is_displayed = 1', array(':language' => $language, ':parent_id' => $category['id']))
                    ->queryAll();

            if (count($cats) > 0) {
                $category['childCategories'] = $cats;
            } else {
                $category['childCategories'] = null;
            }
            $categories[] = $category;
        }
        $this->categories = $categories;
    }
    
    public function getGoogleAnalytics() {
        return "<script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
            ga('create', 'UA-47490505-1', 'needbuy123.com');
            ga('send', 'pageview');
          </script>";
    }

}